/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cart.hibernate.coverImage;

/**
 *
 * @author BibooChung
 */
import cart.hibernate.HibernateUtil;
import cart.hibernate.movieInformation.*;
import java.util.List; 
import java.util.Iterator; 
 
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class ManageCoverImage {
    
    public List getAllImage() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        List allImage = null;
        try{
           tx = session.beginTransaction();
           allImage = session.createQuery("FROM coverImage").list(); 
           tx.commit();
        }catch (HibernateException e) {
           if (tx!=null) tx.rollback();
           e.printStackTrace(); 
        }finally {
           session.close(); 
        }
        return allImage;
    }
    
    
    public long addImage(String imageName, long movieId) { 
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        long imageId = -1;

        try {
            tx = session.beginTransaction();
            MovieInformation movie = (MovieInformation)session.get(MovieInformation.class, movieId); 

            CoverImage image = new CoverImage(imageName, movie);
            imageId = (Long) session.save(image);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return imageId;
    }
    
    public void updateImage(long imageId, String newImageName, long movieId) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();

            CoverImage image = (CoverImage)session.get(CoverImage.class, imageId); 
            image.setImageName(newImageName);

            MovieInformation movie = (MovieInformation)session.get(MovieInformation.class, movieId); 
            image.setMovie(movie);

            session.update(image);
            
            tx.commit();
        }catch (HibernateException e) {
           if (tx!=null) tx.rollback();
           e.printStackTrace(); 
        }finally {
           session.close(); 
        }
    }

    public void deleteImage(long imageId) {
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         CoverImage image = (CoverImage)session.get(CoverImage.class, imageId); 
         session.delete(image); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
    }
}
